1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.apache.tapestry5.ioc.internal;
15
16 import java.lang.annotation.Annotation;
17 import java.lang.reflect.Method;
18
19 import org.apache.tapestry5.ioc.annotations.Advise;
20 import org.apache.tapestry5.ioc.annotations.IntermediateType;
21 import org.apache.tapestry5.plastic.MethodAdvice;
22 import org.apache.tapestry5.plastic.MethodInvocation;
23
24 final public class TestAdvice implements MethodAdvice {
25
26 public static final String ANNOTATION_FOUND = "Annotation found!";
27
28 @Override
29 public void advise(MethodInvocation invocation) {
30
31 final Method method = invocation.getMethod();
32 boolean annotationFoundInMethod = checkAnnotation(method.getAnnotation(Advise.class));
33 boolean annotationFoundThroughAnnotationProvider = checkAnnotation(invocation.getAnnotation(Advise.class));
34 IntermediateType parameterAnnotation = null;
35 final Annotation[][] parameterAnnotations = method.getParameterAnnotations();
36 if (parameterAnnotations.length > 0 && parameterAnnotations[0].length > 0) {
37 parameterAnnotation = (IntermediateType) parameterAnnotations[0][0];
38 }
39 boolean annotationParameter = parameterAnnotation != null && parameterAnnotation.value() == String.class;
40
41 if (annotationFoundInMethod && annotationFoundThroughAnnotationProvider && annotationParameter)
42 {
43 invocation.setReturnValue(ANNOTATION_FOUND);
44 }
45 else {
46 invocation.proceed();
47 }
48
49 }
50
51 private boolean checkAnnotation(Advise annotation)
52 {
53 return annotation != null && "id".equals(annotation.id()) && NonAnnotatedServiceInterface.class.equals(annotation.serviceInterface());
54 }
55
56 }